﻿using System;
using System.Configuration;
using System.Web.Security;
using System.Linq;
using FastTravel.BusinessLogic;
using FastTravel.DB;

namespace FastTravel.IntranetWeb.App_Code
{
	/// <summary>
	/// Провайдер
	/// </summary>
	public class FastTravelMembershipProvider : MembershipProvider
	{
		private string _applicationName = ConfigurationManager.AppSettings["ApplicationName"];

		public override string ApplicationName
		{
			get
			{
				return _applicationName;
			}
			set
			{
				_applicationName = value;
			}
		}

		/// <summary>
		/// Смена пароля
		/// </summary>
		/// <param name="userName">Имя пользователя (логин)</param>
		/// <param name="oldPassword">Старый пароль</param>
		/// <param name="newPassword">Новый пароль</param>
		/// <returns>TRUE - если операция прошла успешно</returns>
		public override bool ChangePassword(string userName, string oldPassword, string newPassword)
		{
			var user = UserManager.Get(userName);
			var changedUser = UserManager.ChangePassword(user.ID, newPassword);
			return changedUser != null;
		}

		public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
		{
			throw new NotImplementedException();
		}

		public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
		{
			throw new NotImplementedException();
		}

		public override bool DeleteUser(string username, bool deleteAllRelatedData)
		{
			throw new NotImplementedException();
		}

		public override bool EnablePasswordReset
		{
			get { return false; }
		}

		public override bool EnablePasswordRetrieval
		{
			get { return false; }
		}

		public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override int GetNumberOfUsersOnline()
		{
			throw new NotImplementedException();
		}

		public override string GetPassword(string username, string answer)
		{
			throw new NotImplementedException();
		}


		/// <summary>
		/// Получить пользователя
		/// </summary>
		/// <param name="userName">Имя пользователя</param>
		/// <param name="userIsOnline">Пользователь онлайн</param>
		/// <returns>Пользователь</returns>
		public override MembershipUser GetUser(string userName, bool userIsOnline)
		{
			var findedUser = UserManager.Get(userName);
			if (findedUser != null)
			{
				var resultUser = new MembershipUser("EasyTravelMembershipProvider",
					findedUser.Login, findedUser.ID, "", "", "", true, false, new DateTime(), new DateTime(),
					new DateTime(), new DateTime(), new DateTime());
				return resultUser;
			}

			return null;
		}

		/// <summary>
		/// Получить пользователя
		/// </summary>
		/// <param name="providerUserKey">Идентификатор пользователя</param>
		/// <param name="userIsOnline">Пользователь онлайн</param>
		/// <returns>Пользователь</returns>
		public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
		{
			//var userList = UserManager.Get(providerUserKey, userIsOnline);
			//User findedUser = null;
			//if (userList.Count() == 1)
			//{
			//    findedUser = userList.First();
			//}

			//if (findedUser != null)
			//{
			//    var resultUser = new MembershipUser("CorrectMembershipProvider",
			//                                                   findedUser.Login, findedUser.ID, "", "", "", true, false, new DateTime(), new DateTime(),
			//                                                   new DateTime(), new DateTime(), new DateTime());
			//    return resultUser;
			//}
			//return null;
			throw new NotImplementedException();
		}

		public override string GetUserNameByEmail(string email)
		{
			throw new NotImplementedException();
		}

		public override int MaxInvalidPasswordAttempts
		{
			get { throw new NotImplementedException(); }
		}

		public override int MinRequiredNonAlphanumericCharacters
		{
			get { throw new NotImplementedException(); }
		}

		public override int MinRequiredPasswordLength
		{
			get { throw new NotImplementedException(); }
		}

		public override int PasswordAttemptWindow
		{
			get { throw new NotImplementedException(); }
		}

		public override MembershipPasswordFormat PasswordFormat
		{
			get { throw new NotImplementedException(); }
		}

		public override string PasswordStrengthRegularExpression
		{
			get { throw new NotImplementedException(); }
		}

		public override bool RequiresQuestionAndAnswer
		{
			get { throw new NotImplementedException(); }
		}

		public override bool RequiresUniqueEmail
		{
			get { throw new NotImplementedException(); }
		}

		public override string ResetPassword(string username, string answer)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Разблокировать пользователя
		/// </summary>
		/// <param name="userName">Имя пользователя</param>
		/// <returns>TRUE - если разблокирован</returns>
		public override bool UnlockUser(string userName)
		{
			var user = UserManager.Block(userName, false);
			return user != null;
		}

		public override void UpdateUser(MembershipUser user)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Проверка пользователя на существование
		/// </summary>
		/// <param name="userName">Имя пользователя (логин)</param>
		/// <param name="password">Пароль</param>
		/// <returns>TRUE - если существует</returns>
		public override bool ValidateUser(string userName, string password)
		{
			return UserManager.Validate(userName, password);
		}

	}
}
